<?php

namespace App\Hospital\Controllers;

use App\Hospital\Repositories\HospitalAppointVaccine;
use App\Models\HospitalAppoint;
use App\Models\HospitalAppointInterval;
use App\Models\Vaccine;
use Dcat\Admin\Form;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Dcat\Admin\Layout\Content;

class HospitalAppointVaccineController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new HospitalAppointVaccine(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('vaccine_id');
            $grid->column('weeks');
            $grid->column('intervals');
            $grid->column('hospital_appoint_id');
            $grid->column('hospital_id');
            $grid->column('week_num');
            $grid->column('interval_num');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    public function list(Content $content,Request $request)
    {
        $data = $request->input();
        $grid = Grid::make(new HospitalAppointVaccine(), function (Grid $grid) use($data) {

            $grid->disableFilter();
            $grid->disableFilterButton();

            $grid->model()->where('hospital_appoint_id', $data['hospital_appoint_id']);


            $grid->column('vaccine_id')->using(Vaccine::query()->pluck('name', 'id')->toArray());
            $grid->column('weeks')->display(function ($model) {
                return json_decode($this->weeks, true);
            })->map(function ($value) {
                $weeks = [
                    '1' => '周一  ',
                    '2' => '周二  ',
                    '3' => '周三  ',
                    '4' => '周四  ',
                    '5' => '周五  ',
                    '6' => '周六  ',
                    '0' => '周日  ',
                ];
                return $weeks[$value];
            })->badge();


            $grid->showCreateButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();

            $grid->setResource('hospital-appoint-vaccine');
            $grid->disableFilter();

            $grid->setActionClass(Grid\Displayers\Actions::class);
            $grid->showQuickEditButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();

            $grid->model()->setConstraints([
                'hospital_appoint_id' => $data['hospital_appoint_id'],
                'hospital_id' => $data['hospital_id'],
            ]);
        });
        return $content->full()->body($grid);
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HospitalAppointVaccine(), function (Show $show) {
            $show->field('id');
            $show->field('vaccine_id');
            $show->field('weeks');
            $show->field('intervals');
            $show->field('hospital_appoint_id');
            $show->field('hospital_id');
            $show->field('week_num');
            $show->field('interval_num');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HospitalAppointVaccine(), function (Form $form) {
            $hospital_appoint_id = request()->input('hospital_appoint_id');
            $hospital_id = request()->input('hospital_id');
// 去除整个工具栏内容
$form->disableHeader();
$form->disableViewCheck();

            $form->tab('疫苗设置', function (Form $form) use($hospital_appoint_id,$hospital_id) {
                $hospitalAppoint = HospitalAppoint::query()->where('id',$hospital_appoint_id)->first();
                if(isset($hospitalAppoint) && $hospitalAppoint->type == 2){
                    $vaccines = Vaccine::query()
                        ->join('eb_vaccine_type', 'eb_vaccine_type.id', '=', 'eb_vaccine.type_id')
                        ->where('eb_vaccine_type.crowd',1)
                        ->pluck('eb_vaccine.name','eb_vaccine.id')->toArray();
                }else{
                    $vaccines = Vaccine::query()
                        ->join('eb_vaccine_type', 'eb_vaccine_type.id', '=', 'eb_vaccine.type_id')
                        ->where('eb_vaccine_type.crowd',2)
                        ->pluck('eb_vaccine.name','eb_vaccine.id')->toArray();
                }

                $form->html('设置后疫苗仅可在已选门诊日预约');
                $form->select('vaccine_id')->options($vaccines)->required();
                $form->html('注：如不需要用户预约时选择具体疫苗，那么此处选择“儿童疫苗”即可');
                $form->multipleSelect('weeks','门诊日')->options([
                    '1' => '周一  ',
                    '2' => '周二  ',
                    '3' => '周三  ',
                    '4' => '周四  ',
                    '5' => '周五  ',
                    '6' => '周六  ',
                    '0' => '周日  ',
                ])->saving(function ($value) {
                    return json_encode($value);
                })->required();
                $form->hidden('hospital_appoint_id')->value($hospital_appoint_id);
                $form->hidden('hospital_id')->value($hospital_id);

            })->tab('疫苗门诊日号源', function (Form $form) {
                $form->html('设置后用户选择此疫苗时门诊日最多可约数量，且不会超过该项目门诊日的总数，未设置则按照门诊日总数判断');

                $form->embeds('week_num','', function ($form) {
                    $form->text('week1')->placeholder('请输入数量');
                    $form->text('week2')->placeholder('请输入数量');
                    $form->text('week3')->placeholder('请输入数量');
                    $form->text('week4')->placeholder('请输入数量');
                    $form->text('week5')->placeholder('请输入数量');
                    $form->text('week6')->placeholder('请输入数量');
                    $form->text('week0')->placeholder('请输入数量');
                })->saving(function ($value) {
                    return json_encode($value);
                });
            })->tab('疫苗门诊日时段号源', function (Form $form) use($hospital_appoint_id,$hospital_id){

                $intervals = HospitalAppointInterval::query()->where('hospital_appoint_id',$hospital_appoint_id)->get();
                if($intervals){
                    $typeText=[];
                    foreach($intervals as $k=>$v){
                        $typeText[$v->id]=$v->start."-".$v->end;
                    }
                    $weeks = [1, 2, 3, 4, 5, 6, 0];

                    $row = [];
                    foreach($typeText as $k=>$v){
                        $row[$k]['title']=$v;
                        $row[$k]['key']=$k;

                        foreach($weeks as $wk=>$wv){
                            $wArr[$wv]['open']=0;
                            $wArr[$wv]['value']="";
                            $wArr[$wv]['key']=$wv;
                        }
                        $row[$k]['weeks']=$wArr;
                    }
                }
                $form->intervalweeknum('interval_num')->value($row);
                $form->html('设置后用户选择此疫苗时门诊日时段最多可约数量，且不会超过该项目时段、门诊日的总数，未设置则按照门诊日总数判断');
                $form->html('勾选开启，表示选择该疫苗时只能预约已勾选开启的时段');
                $form->html('勾选开启，数量设置为空则不限制（以项目时间段号源为准），为0则不可约，为10则该疫苗只能在此时段预约10个');

            });

        });
    }
}
